#include <QDebug>
#include "Include/IconProvider.h"
#include "BottomDockTitleWidget.h"
#include "ui_BottomDockTitleWidget.h"

BottomDockTitleWidget::BottomDockTitleWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BottomDockTitleWidget)
{
    ui->setupUi(this);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    connect(ui->toggleGroup, SIGNAL(buttonClicked(QAbstractButton*)), SLOT(btnToggled(QAbstractButton*)));
    connect(ui->showHideBtn, SIGNAL(toggled(bool)), SLOT(onShowHideToggled(bool)));

    ui->showHideBtn->setIcon(IconProvider::instance()->icon("go-up"));
    ui->runBtn->setIcon(IconProvider::instance()->icon("system-run"));
    ui->searchBtn->setIcon(IconProvider::instance()->icon("search"));
    ui->errorBtn->setIcon(IconProvider::instance()->icon("errors"));
    connect(ui->stopRunBtn, SIGNAL(clicked()), SIGNAL(stopProject()));
}

BottomDockTitleWidget::~BottomDockTitleWidget()
{
    delete ui;
}

void BottomDockTitleWidget::btnToggled(QAbstractButton* btn)
{
    if (btn == ui->runBtn)
        emit modeSelected(ShowRun);
    if (btn == ui->searchBtn)
        emit modeSelected(ShowSearch);
    if (btn == ui->errorBtn)
        emit modeSelected(ShowErrors);

    ui->showHideBtn->setChecked(btn->isChecked());
}

void BottomDockTitleWidget::onShowHideToggled(bool toggled)
{
    ui->showHideBtn->setIcon(IconProvider::instance()->icon(toggled ? "go-down" : "go-up"));
    emit modeSelected(toggled ? Show : Hide);
}

void BottomDockTitleWidget::dockModeSelected(DockShowMode mode)
{
    switch(mode)
    {
    case ShowRun:
        ui->runBtn->setChecked(true);
        ui->showHideBtn->setChecked(true);
        break;
    case ShowSearch:
        ui->searchBtn->setChecked(true);
        ui->showHideBtn->setChecked(true);
        break;
    case ShowErrors:
        ui->errorBtn->setChecked(true);
        ui->showHideBtn->setChecked(true);
        break;
    default:
        break;
    }
}

void BottomDockTitleWidget::projectRunned(bool runned)
{
    ui->stopRunBtn->setEnabled(runned);
}
